package com.tp.ssm.controller.interceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

public class LoginInterceptor implements HandlerInterceptor{

	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		String uri = request.getRequestURI() ;
		System.out.println(uri);
		//判断请求地址是不是公开地址，如果是就放行
		if(uri.indexOf("login.action") >= 0){
			return true ;
		}
		
		//如果session中有username，就放行
		HttpSession session = request.getSession() ;
		if(session.getAttribute("username") != null){
			return true ;
		}
		request.getRequestDispatcher("/WEB-INF/jsp/login.jsp").forward(request, response); ;
		
		return false;
	}

	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		// TODO 自动生成的方法存根
		
	}

	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		// TODO 自动生成的方法存根
		
	}

}
